﻿using System.Collections.Generic;
using System.Linq;
using compshop.Infrastructure;
using compshop.Infrastructure.CompShopDataService;
using compshop.Modules.ClientOrderList.Views;
using System;

namespace compshop.Modules.ClientOrderList.Presenter
{
    public class ClientOrderListPresenter
    {
        private CompShopEntities contextDS;
        private EventsManager eventsManager;

        public IViewClientOrderList View { set; get; }
        public IList<ClientOrder> ClientOrders { set; get; }

        public ClientOrderListPresenter(EventsManager eventsManager)
        {
            contextDS = CompShopEntitiesContext.GetInstance();
            this.eventsManager = eventsManager;
            eventsManager.clientOrderListPresenter = this;
            if(Departments.Count!=0)
            ClientOrders = GetClientOrders(0,Departments[0]);
            else
                ClientOrders = GetClientOrders(0, null);
        }

        public IList<ClientOrder> GetClientOrders(int status,Department department)
        {
            var query =
                    from co in contextDS.ClientOrders
                    select co;

            if (status != 0)
                query =
                   from co in query
                   where co.Status == status
                   select co;
            if(department!=null)
            query =
                  from co in query
                  where co.DepartmentID == department.DepartmentID
                  select co;

            return query.ToList<ClientOrder>(); 
        }

        public IList<string> ClientOrderStatuses
        {
            get
            {
                return Status.StatusClientOrder;
            }
        }

        public IList<Department> Departments
        {
            get 
            { 
                return contextDS.Departments.ToList(); 
            }
        }

        public void ShowNewClientOrderDialog()
        {
            NewClientOrderDialog dialog = new NewClientOrderDialog(this);
            if (dialog.ShowDialog() == true)
            {
                try
                {
                    dialog.ClientOrder.CreationDate = DateTime.Now;
                    dialog.ClientOrder.Status = 1;
                    contextDS.AddToClientOrders(dialog.ClientOrder);
                    ClientOrders.Add(dialog.ClientOrder);
                    contextDS.SaveChanges();
                    View.UpdateData();
                    onSelectedClientOrder((int)dialog.ClientOrder.ClientOrderID);
                }
                catch (System.Exception ex)
                {
                    System.Windows.MessageBox.Show(Resource.msgSaveToDBError + "\n" + ex.Message);
                }
            }
        }

        public void ClienOrderAnnulate()
        {
            YesNoDialog dialog = new YesNoDialog(Resource.msgIsSureInAnnulation);
            if (dialog.ShowDialog() == true)
            {
                //снятие резерва, аннуляция заказа и связанных с ними заявок на доставку
            }
        }


        #region Publishers

        public void onSelectedClientOrder(int id)
        {
            eventsManager.EventAggregator.GetEvent<ClientOrderSelectedEvent>().Publish(id);
        }

        public void CreateOrderEditWindow()
        {
            eventsManager.EventAggregator.GetEvent<ShellChangeEvent>().Publish(ShellStates.GetOrderProductEdit);
        }

        #endregion

        #region Handlers
        public void SetSelectedClientOrder(int id)
        {

        }
        #endregion

    }
}
